package com.lhl.fw.core.data.converter;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.AttributeConverter;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @className: com.lhl.fw.core.data.converter.ListStringToTextConverter
 * @description: 自定义 hibernate 的 String 转 List<String> 的 converter,对应数据库的 varchar 类型
 * @author: king
 * @date: 2020-11-16
 **/
@Api(tags = "ListStringToTextConverter", produces = "")
@Slf4j
public class ListStringToTextConverter implements AttributeConverter<List<String>, String>, Serializable {

    @Override
    public String convertToDatabaseColumn(List<String> attribute) {
        if (Objects.isNull(attribute)) return null;
        return JSONUtil.toJsonStr(attribute);
    }

    @Override
    public List<String> convertToEntityAttribute(String dbData) {
        if (Objects.isNull(dbData)) return null;
        return JSONUtil.toBean(dbData, List.class);
    }
}
